Copy & Clone Triats

Both are used for duplicating the values.
All types that implement Copy must also implement Clone, but the reverse is not true
Copy (Shallow Copy) Clone (Deep Copy)
What Type implementing(copy triat) can be duplicated simply by Bitwise Copying For types that require more complex duplication logic.
Efficient More efficient since it involves a simple bitwise copy May involve more complex operations and potentially higher overhead
Used for Simple scalar types(Eg: int, struct containing simple types) For complex datatypes which manage resources on heap(like String, Vec, etc)
Example

// Only Copy triat is used, but to use
// Copy, we need to derive Clone
#[derive(Copy, Clone)]
pub struct S {
    x : i32,
    y : i32,
}
fn main() {
    let a = S {
        x: 1,
        y: 5,
    };
    let b = a;
    println!("b = {}, {}", b.x, b.y);
}
                

#[derive(Clone)]
pub struct S {
    x : Vec <i32>,
}
fn main() {
    let a = S {
        x: vec![1,2,3],
    };
    let b = a;
    for i in 0..b.x.len() {
        println!("{} ", b.x[i]);
    }
}
                    

Default trait

Used to give default value
#[derive(Default)] Default value by user
Rust to provide Default implementation

#[derive(Default)]
pub struct Test {
    a : i32,
}
fn main() {
    let test1: Test = Default::default();
    println!("{}",test1.a);
}
$ cargo run
0        //0 is initialized by rust system
                    
User overrides the default() function provided by Default Trait

pub struct test {
    a : i32,
}
impl Default for test{
    fn default() -> Self {
        Self {
            a: 48,
        }
    }
}
fn main() {
    let test1: Test = Default::default();
    println!("{}",test1.a);             //48
}
                    

drop trait

Custom code within the destructor.
When a value is no longer needed, Rust runs a “destructor” on that value, but if we want to perform some task in destructor, use drop trait.

#[derive(Default)]
pub struct S {
    x : i32,
}
impl Drop for S {
    fn drop(&mut self){
        println!("Dropping");
    }
}
fn main() {
    {
        let s:S = Default::default();
        println!("{}", s.x);
    }
    println!("After scope");
}
$ cargo run
0
Dropping
After scope
        

FnOnce trait (Function that can be called Once)

pub fn foo <F> (&self, arg1: F)
where
    F: FnOnce(), {
    }
//F is a generic type parameter (like template <typename F> in C++)

A trait in Rust for closures or functions that can be called at least once, but might consume the variables they capture from their environment.
• Takes Ownership: It moves (consumes) captured variables by value.
• Called Once: Because the captured data is moved out during the call, the closure destroys itself or loses its data, making a second call impossible.

fn main() {
    let text = String::from("Hello, Rust!");

    // This closure takes ownership of `text` because of `move`
    let consume_string = move || {
        println!("{}", text);
        // `text` is consumed/dropped here when the closure runs
    };

    // First call works fine
    consume_string();

    // Second call causes a compile-time error!
    // consume_string(); 
}

future trait

future is a "asynchronous value" that may not have finished computing yet. Thread waits on future to become available. These are similar to epoll(), not smilar to poll() or select().

use std::pin::Pin;
use std::task::Context;

pub trait Future {
    type Output;
    fn poll(self: Pin <&mut Self>, cx: &mut Context <'_>) -> Poll <Self::Output>;
}
pub enum Poll <T> {
    Ready(T),
    Pending,
}            
        

Iterator trait

Used for processing sequences of data, allowing you to traverse or consume elements one by one.
With Iterators only Rust handles collections, streams of data, and other sequential operations.

pub trait Iterator {
    type Item;  // The type of the elements being iterated over.

    // The Iterator trait requires one method to be implemented
    // This method advances the iterator and returns the next item in the sequence wrapped in Option
    fn next(&mut self) -> Option <Self::Item>;
}
        

Send, Sync trait

Send Sync
What Types (T) that can be transferred across thread boundaries. Types for which it is safe to share references (&T) between threads.
unsafe trait Yes Yes
Automatically derived trait Yes
if type is thread-safe, then it Automatically derived Send,sync
Apart from Rc and UnsafeCell, all types are Send, Sync.
Yes
Can be implemented Yes

struct MyBox(*mut u8);
unsafe impl Send for MyBox {}
                    
Yes

struct MyBox(*mut u8);
unsafe impl Sync for MyBox {}
                    
Can be unimplemented Yes

#![feature(negative_impls)]

// I have some magic semantics for some synchronization primitive!
struct SpecialThreadToken(u8);

impl !Send for SpecialThreadToken {}
                    
Yes

impl !Sync for SpecialThreadToken {}